package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.NewSuperiorProductsAdapter;
import com.dingwei.superiordiary.bean.GoodsListBean;
import com.dingwei.superiordiary.mvp.presenter.NewSuperiorProductsPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/1/24.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class NewSuperiorProductsActivity extends BaseActivity implements Contract.NewSuperiorProductsView {

    NewSuperiorProductsPresenter newSuperiorProductsPresenter = new NewSuperiorProductsPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;


    private NewSuperiorProductsAdapter mAdapter;
    private String title = "新优品";
    private int page = 1;
    private String category_id = "";
    private String order = "";
    private String keyword  ="";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_new_superior_products;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    protected void initView() {

        if (getIntent().hasExtra("activity_title")) {
            title = getIntent().getStringExtra("activity_title");
        }
        if (getIntent().hasExtra("category_id")) {
            category_id = getIntent().getStringExtra("category_id");
        }
        if (getIntent().hasExtra("order")) {
            order = getIntent().getStringExtra("order");
        }

        if (getIntent().hasExtra("keyword")) {
            keyword = getIntent().getStringExtra("keyword");
        }

        tvTitle.setText(title);

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mAdapter = new NewSuperiorProductsAdapter(BaseActivity.context);
        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                newSuperiorProductsPresenter.initData(page, category_id, order,keyword, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                newSuperiorProductsPresenter.initData(page, category_id, order, keyword,true, false);
            }
        });

        mAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                intent  = new Intent(BaseActivity.context, CommodityDetailsActivity.class);
                GoodsListBean.DataBean bean = mAdapter.getItem(position);
                intent.putExtra("id",bean.id);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onFail() {
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
        showData("fail");
    }

    @Override
    public void onLoadMore(GoodsListBean bean) {
        mAdapter.addAll(bean.data);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void onRefresh(GoodsListBean bean) {
        mAdapter.clear();
        mAdapter.addAll(bean.data);
        refreshLayout.finishRefresh();
        showData("success");
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (mAdapter.getAllData() != null && mAdapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.img_back, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.no_net_btn:
                page = 1;
                newSuperiorProductsPresenter.initData(page,category_id,order,keyword,true,true);
                break;
        }
    }
}
